Contents Prev Next Up


2.3 Constant Pool

Each item in the constant pool begins with a 1-byte tag:. The table below lists the valid tags and their values.
 
Constant TypeValue
CONSTANT_Class7
CONSTANT_Fieldref9
CONSTANT_Methodref10
CONSTANT_InterfaceMethodref11
CONSTANT_String8
CONSTANT_Integer3
CONSTANT_Float4
CONSTANT_Long5
CONSTANT_Double6
CONSTANT_NameAndType12
CONSTANT_Utf81
CONSTANT_Unicode2

Each tag byte is then followed by one or more bytes giving more information about the specific constant.

CONSTANT_Class

CONSTANT_Class is used to represent a class or an interface.

CONSTANT_Class_info {
 u1 tag;
 u2 name_index;
}

tag

The tag will have the value CONSTANT_Class

name_index

constant_pool[name_index] is a CONSTANT_Utf8 giving the string name of the class.

Because arrays are objects, the opcodes anewarray and multianewarray can reference array "classes" via CONSTANT_Class items in the constant pool. In this case, the name of the class is its signature. For example, the class name for

int[][] 
is

[[I

The class name for

Thread[] 
is

"[Ljava.lang.Thread;"

CONSTANT_{Fieldref,Methodref,InterfaceMethodref}

Fields, methods, and interface methods are represented by similar structures.

CONSTANT_Fieldref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}

CONSTANT_Methodref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}

CONSTANT_InterfaceMethodref_info {
 u1 tag;
 u2 class_index;
 u2 name_and_type_index;
}

tag

The tag will have the value CONSTANT_Fieldref, CONSTANT_Methodref, or CONSTANT_InterfaceMethodref.

class_index

constant_pool[class_index] will be an entry of type CONSTANT_Class giving the name of the class or interface containing the field or method.

For CONSTANT_Fieldref and CONSTANT_Methodref, the CONSTANT_Class item must be an actual class. For CONSTANT_InterfaceMethodref, the item must be an interface which purports to implement the given method.

name_and_type_index

constant_pool[name_and_type_index] will be an entry of type CONSTANT_NameAndType. This constant pool entry indicates the name and signature of the field or method.

CONSTANT_String

CONSTANT_String is used to represent constant objects of the built-in type String.

CONSTANT_String_info {
 u1 tag;
 u2 string_index;
}

tag

The tag will have the value CONSTANT_String

string_index

constant_pool[string_index] is a CONSTANT_Utf8 string giving the value to which the String object is initialized.

CONSTANT_Integer and CONSTANT_Float

CONSTANT_Integer and CONSTANT_Float represent four-byte constants.


CONSTANT_Integer_info {
 u1 tag;
 u4 bytes;
}

CONSTANT_Float_info {
 u1 tag;
 u4 bytes;
}

tag

The tag will have the value CONSTANT_Integer or CONSTANT_Float

bytes

For integers, the four bytes are the integer value. For floats, they are the IEEE 754 standard representation of the floating point value. These bytes are in network (high byte first) order.

CONSTANT_Long and CONSTANT_Double

CONSTANT_Long and CONSTANT_Double represent eight-byte constants.

CONSTANT_Long_info {
 u1 tag;
 u4 high_bytes;
 u4 low_bytes;
}

CONSTANT_Double_info {
 u1 tag;
 u4 high_bytes;
 u4 low_bytes;
}

All eight-byte constants take up two spots in the constant pool. If this is the nth item in the constant pool, then the next item will be numbered n+2.

tag

The tag will have the value CONSTANT_Long or CONSTANT_Double.

high_bytes, low_bytes

For CONSTANT_Long, the 64-bit value is (high_bytes << 32) + low_bytes.

For CONSTANT_Double, the 64-bit value, high_bytes and low_bytes together represent the standard IEEE 754 representation of the double-precision floating point number.

CONSTANT_NameAndType

CONSTANT_NameAndType is used to represent a field or method, without indicating which class it belongs to.

CONSTANT_NameAndType_info {
 u1 tag;
 u2 name_index;
 u2 signature_index;
}

tag

The tag will have the value CONSTANT_NameAndType.

name_index

constant_pool[name_index] is a CONSTANT_Utf8 string giving the name of the field or method.

signature_index

constant_pool[signature_index] is a CONSTANT_Utf8 string giving the signature of the field or method.

CONSTANT_Utf8 and CONSTANT_Unicode

CONSTANT_Utf8 and CONSTANT_Unicode are used to represent constant string values.

CONSTANT_Utf8 strings are "encoded" so that strings containing only non-null ASCII characters, can be represented using only one byte per character, but characters of up to 16 bits can be represented:

All characters in the range 0x0001 to 0x007F are represented by a single byte:

     +-+-+-+-+-+-+-+-+
     |0|7bits of data|
     +-+-+-+-+-+-+-+-+

The null character (0x0000) and characters in the range 0x0080 to 0x07FF are represented by a pair of two bytes:

     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
     |1|1|0| 5 bits |   |1|0|  6 bits   |
     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+

Characters in the range 0x0800 to 0xFFFF are represented by three bytes:

     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+
     |1|1|1|0|4 bits |   |1|0|  6 bits   |   |1|0|  6 bits   |
     +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+   +-+-+-+-+-+-+-+-+

There are two differences between this format and the "standard" UTF-8 format. First, the null byte (0x00) is encoded in two-byte format rather than one-byte, so that our strings never have embedded nulls. Second, only the one-byte, two-byte, and three-byte formats are used. We do not recognize the longer formats.

CONSTANT_Utf8_info {
 u1 tag;
 u2 length;
 u1 bytes[length];
}

CONSTANT_Unicode_info {
 u1 tag;
 u2 length;
 u2 bytes[length];
}

tag

The tag will have the value CONSTANT_Utf8 or CONSTANT_Unicode.

length

The number of bytes in the string. These strings are not null terminated.

bytes

The actual bytes of the string.


Contents Prev Next Up